
import wilberforce.events.simpleEventHelper;
//import wilberforce.ui.forms.abstractScrollbar;
import wilberforce.util.drawing.drawingUtility;
import com.bourre.events.BasicEvent;
import com.bourre.events.EventType;
import wilberforce.events.UIEvents;
import com.bourre.commands.Delegate;
import wilberforce.util.textField.textFieldUtility;
import wilberforce.ui.forms.abstractScrollbar;

/**
 * @author Simon
 */ 
class wilberforce.ui.forms.ListBox extends simpleEventHelper {
	
	private var container:MovieClip;	private var backgroundContainer:MovieClip;
	private var data:Array;
	
	private var scrollBarContainer:MovieClip;	private var scrollBar:abstractScrollbar;
	private var itemWidth:Number;
	private var itemHeight:Number;
	private var visibleRows:Number;	private var maxNecessaryHeight:Number;
	
	private var labelClips:Array;
	private var lineHolderClip:MovieClip;
	private var labelHolderMC:MovieClip;
	
	public var selectedIndex:Number;
	private var currentSelectedClip:MovieClip;
	var currentOffset:Number;
	
	public static var backgroundLineColor:Number=0x333333;	public static var backgroundLineAlpha:Number=100;
	public static var backgroundFillColor:Number=0xFFFFFF;	public static var backgroundFillAlpha:Number=100;
	
	var lineColor1:Number=0x000000;
	var lineAlpha1:Number=15;
		
	//var lineColor2:Number=0xFFFFFF;
	//var lineAlpha2:Number=50;
	
	public static var labelTextFormat:TextFormat=new TextFormat("_sans",12,0x0000,false);	public static var DEFAULT_WIDTH:Number=100;	public static var DEFAULT_HEIGHT:Number=200;	public static var SCROLLBAR_MARGIN:Number=1;
	
	public function ListBox(container:MovieClip) {
		super();
		this.container=container;
		//trace("Added to "+this.container);
		
		initialise();
	}

	/** Adds an item to the listBox.
	* @param tLabel The label to appear within the listBox
	* @param tData The data associated with the item. Can be any type
	*/
	public function addItem(tLabel:String,tData:Object):Void {
		data.push({label:tLabel,data:tData});
		scrollBar.setProperties(visibleRows,data.length);
		displayData(0);
	}
	
	/** When all objects are ready, this initialises clip and fires off the corresponding event
	* to all listeners.
	*/
	private function initialise():Void {
		data=new Array();
		itemHeight=labelTextFormat.size+8;
		backgroundContainer=container.createEmptyMovieClip("backgroundContainer",container.getNextHighestDepth());
		labelHolderMC=container.createEmptyMovieClip("labelHolder",container.getNextHighestDepth());
		lineHolderClip=container.createEmptyMovieClip("lineHolder",container.getNextHighestDepth());
		scrollBarContainer=container.createEmptyMovieClip("scrollBarContainer",container.getNextHighestDepth());
		scrollBar=new abstractScrollbar(scrollBarContainer,DEFAULT_HEIGHT);
		setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
		
		scrollBar.setProperties(1,1);
		scrollBar.addListener(this);
				
		dispatchEvent(new BasicEvent(UIEvents.UI_COMPONENT_INITIALISED,this));		
	}
	
	/** Sets the size of the component and re-renders
	* @param tWidth The new width of the component
	* @param tHeight The new height of the component
	*/
	function setSize(tWidth:Number,tHeight:Number):Void {
		
		// Render the background
		backgroundContainer.clear();
		drawingUtility.drawRectangle(backgroundContainer,0,0,tWidth,tHeight,backgroundLineColor,backgroundLineAlpha,backgroundFillColor,0,backgroundFillAlpha);
		
			
		//if (tHeight>maxNecessaryHeight) tHeight=maxNecessaryHeight;
		var tRemainingWidth:Number=scrollBar.x=tWidth-scrollBar.width;
		scrollBar.y=0;
		itemWidth=tRemainingWidth-SCROLLBAR_MARGIN;
				
		// Work out the number of items we can add
		visibleRows=Math.floor(tHeight/itemHeight);
		
		
		var maxscroll=data.length-visibleRows;
		if (currentOffset>maxscroll) {
			currentOffset=maxscroll;
		}
		
		scrollBar.setHeight(tHeight);
		scrollBar.setProperties(visibleRows,data.length);
		scrollBar.setValue(currentOffset);
		// Redisplay the data
		displayData(currentOffset);
	//	scrollBar.setValue(0);
	}
	
		/** Sets the current dislpay offset of the listBox (the index appearing at the top of the list)
	* @param tOffset The index of the item to appear at the top of the list
	*/
	function setPosition(tOffset:Number) {
		//trace("Setting position to "+tOffset);
		selectedIndex=tOffset;
		currentOffset=tOffset;
		
		scrollBar.setValue(currentOffset);
		displayData(currentOffset);
	}
	
	/** Allows the data for the component to be set  */	
	function setData(tArray:Array,tSelectedIndex) {
		
		//trace("PASSED ARRAY");		//trace("============");
		for (var i in tArray) trace(tArray[i].label);
		
		selectedIndex=-1;
		if (tSelectedIndex!=undefined) selectedIndex=tSelectedIndex;
		currentSelectedClip=null;
		data=tArray;
		//trace("Selected index "+selectedIndex);
		
		scrollBar.setProperties(visibleRows,tArray.length);
		
		var tOffset=tSelectedIndex;
		
		// Check to see if tOffset it higher than the maximum possible
		if (tOffset>=(data.length-visibleRows)) {
			tOffset=data.length-visibleRows;
		}
		
		if (tOffset<0) tOffset=0;
		scrollBar.setValue(tOffset);
		displayData(tOffset);
		maxNecessaryHeight=tArray.length*itemHeight;
		
		//trace("Data "+tArray.length);
		
	}
	/** Renders the listBox  */	
	private function displayData(tOffset) {
		//trace("DIsplaying offset "+tOffset);
		currentOffset=tOffset;
		for (var i in labelClips) labelClips[i].removeMovieClip();
		lineHolderClip.clear();
		labelClips=[];
		var tx:Number=0;
		var ty:Number=0;
		
		//trace("Rendering "+visibleRows+","+data.length);
		
		// Draw the top line
		lineHolderClip.moveTo(0,0);
		lineHolderClip.lineStyle(0,lineColor1,lineAlpha1);
		lineHolderClip.lineTo(itemWidth,0);
		
		//lineHolderClip.lineStyle(0,lineColor2,lineAlpha2);
		//lineHolderClip.moveTo(0,1);		
		//lineHolderClip.lineTo(itemWidth,1);
		
		
		for (var i=0;i<visibleRows && i<data.length;i++) {
			var tDataIndex=i+tOffset;
			var tData=data[tDataIndex];
			//trace("Creating label "+tData.label);
			var tLabelClip=createLabelClip(tx,ty,tData.label,tData.data,tDataIndex);
			labelClips.push(tLabelClip);
			ty+=itemHeight;
			
			// Draw the top line
			lineHolderClip.moveTo(0,ty);
			lineHolderClip.lineStyle(0,lineColor1,lineAlpha1);
			lineHolderClip.lineTo(itemWidth,ty);
			
			//lineHolderClip.moveTo(0,ty+1);
			//lineHolderClip.lineStyle(0,lineColor2,lineAlpha2);
			//lineHolderClip.lineTo(itemWidth,ty+1);
		}
	}
	
	/** Creates an individual label item  */
	private function createLabelClip(tx,ty,tLabel,tData,tDataIndex) {
		
		var tDepth:Number=labelHolderMC.getNextHighestDepth();		
		var tLabelClip:MovieClip=labelHolderMC.createEmptyMovieClip("labelHolder"+tDepth,tDepth);
		
		tLabelClip.data=tData;
		tLabelClip.index=tDataIndex;
		
		var tButtonClip:MovieClip=tLabelClip.createEmptyMovieClip("labelButton",1);
		var tData={_x:itemWidth-10,_y:itemHeight/2};
		//var tArrowClip:MovieClip=tLabelClip.attachMovie("sceeListBoxArrow","arrow",2,tData);
		drawingUtility.drawRectangle(tButtonClip,0,0,itemWidth,itemHeight,0x000000,0x000000,0xDDDDDD,undefined,70);
		
		
		if (selectedIndex==tDataIndex) {
			tLabelClip.selected=true;
			currentSelectedClip=tLabelClip;
		}
		else {
			tLabelClip.selected=false;
			tButtonClip._alpha=0;
		}
		
		tButtonClip.onPress= Delegate.create(this,itemSelected,tLabelClip,tDataIndex, tData);		tButtonClip.onRollOver= Delegate.create(this,rollOverItem,tLabelClip,tButtonClip);		tButtonClip.onRollOut= Delegate.create(this,rollOutItem,tLabelClip,tButtonClip);
		
		

		
		
		tLabelClip._x=tx;
		tLabelClip._y=ty;
		// Create the text field
		// Currently the style is baked. this needs to be removed
		var tTextField:TextField=textFieldUtility.createTextField(tLabelClip,0,0,itemWidth,itemHeight,labelTextFormat,tLabel,false,false,true);
		tTextField._width=itemWidth;
		
		//trace("adding "+tLabel+" clip "+tLabelClip+" info "+tCreationData.height+" width "+itemWidth+" height "+itemHeight);
		return tLabelClip;
	}
	
	function rollOverItem(tLabelClip:MovieClip,tButtonClip:MovieClip):Void
	{
		tButtonClip._alpha=100;
		
		
	}
	
	function rollOutItem(tLabelClip:MovieClip,tButtonClip:MovieClip):Void
	{
		//trace(tLabelClip.selected+" - "+tLabelClip);
		if (!tLabelClip.selected) tButtonClip._alpha=0;
		
	}
	
	
	/** Called when an individual item is selected */
	function itemSelected(tLabelClip:MovieClip,index:Number,tData) {
		
		selectedIndex=index;
		dispatchEvent(new BasicEvent( UIEvents.UI_COMPONENT_SELECTION_CHANGED,this));
		
		/*
		for (var i=0;i<listenerObjects.length;i++) {
			listenerObjects[i].listBoxSelected(index,tData);
		}
		*/
		currentSelectedClip.selected=false;		
		//currentSelectedClip.arrow._visible=false;
		currentSelectedClip.labelButton._alpha=0;
		
		currentSelectedClip=tLabelClip;
		tLabelClip.selected=true;
		//trace("Setting true for "+tLabelClip);
	}
	
	public function setSelectedIndex(index:Number):Void
	{
		
		selectedIndex=index;
		// Update display - TODO
	}
	
	/** Called when an scrollBar has moved */
	function onScroll(e:BasicEvent)
	{
		displayData(Math.round(e.getTarget()));
		//tClip,currentValue) {
		//trace("currentValue is "+currentValue)
		//currentValue=Math.round(currentValue);
		//displayData(currentValue);
		
	}
}